home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 439_01 / dontread.me < prev    next >
Text File  |  1995-02-10  |  52KB  |  886 lines

  1. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. ;+                                                                             +
  3. ;+                        DONTREAD.ME Version 2.0                              +
  4. ;+                 A Tutorial for XLIB and Protected Mode                      +
  5. ;+                                                                             +
  6. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  7. ;
  8. ;   If you don't read much, then this file was made just for you.  It's 
  9. ;a lot shorter than XLIB.DOC.  It also explains several protected-mode 
  10. ;concepts, including some that are needed to understand XLIB.DOC.  We work
  11. ;through an assembly language program which demonstrates the usage of XLIB.
  12. ;As we go, we are going to do a lot of talking about protected mode in 
  13. ;general as well as protected mode under XLIB in particular.
  14. ;   First, let's talk about what XLIB is generally designed to do:  XLIB 
  15. ;will allow you to write protected-mode procedures using your familiar 
  16. ;language development tools (compilers, linkers, etc), and will allow you to
  17. ;execute code containing such procedures using DOS.  XLIB is also designed
  18. ;to make all this as simple for you as it possibly can.  That turns out to
  19. ;very simple indeed.
  20. ;   XLIB has one major shortcoming:  It relies on DOS to load executables
  21. ;and files.  This means that our code must reside in the first meg because
  22. ;that's the only thing DOS knows how to work with.  It also means that when
  23. ;we transfer disk files to or from extended memory, we must perform a 
  24. ;transfer through a buffer in the first meg.  In almost every other regard, 
  25. ;we will have unlimited power.  Most importantly, we will be able to access 
  26. ;data in extended memory with extreme ease and speed.
  27. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  28.  
  29. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  30. ;   Now, let's start with a little program.  We must begin with a model 
  31. ;declaration.  XLIB is a large-model library which uses PASCAL conventions.  
  32. ;We will use the same model here; however, this is not a requirement of XLIB.
  33. ;We will use the FARSTACK option because this ensures that our real-mode
  34. ;stack will be at the end of the program.  This is where it should be under
  35. ;most circumstances anyway, but TASM doesn't always put it there (see the
  36. ;section on bugs in README.DOC).  FARSTACK allows us to conclude that the
  37. ;initial SS:SP points to the end of the program.  This will become useful
  38. ;information a few lines down.  We also want to tell the assembler to let us 
  39. ;use 32-bit instructions.  We do this with the .386 directive.
  40. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  41.  
  42.                .MODEL         LARGE,PASCAL,FARSTACK
  43.                .386
  44.  
  45. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  46. ;   The next two line are the key ingredients.  We want to provide XLIB 
  47. ;symbols to this program with XLIB.INC.  We also want to link with XLIBE.LIB.  
  48. ;XLIBE.LIB has exception trapping capabilities whereas XLIB.LIB does not.
  49. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  50.  
  51.                INCLUDE        XLIB.INC
  52.                INCLUDELIB     XLIBE.LIB
  53.  
  54. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  55. ;TASM Only:
  56. ;
  57. ;   If you are a TASM programmer, then don't use the last two lines.  Use the
  58. ;following lines instead.  These lines include some useful macros that should 
  59. ;have been in TASM to begin with.
  60. ;
  61. ;              MASM51                        ;Emulate MASM51
  62. ;              QUIRKS                        ;MASM51 quirks are sometimes nice
  63. ;
  64. ;PUSHW         MACRO IMMEDIATE16:REST        ;PUSH 16-bit constant
  65. ;              IF (@WordSize EQ 4)
  66. ;              DB             66H
  67. ;              ENDIF
  68. ;              DB             68H
  69. ;              DW             IMMEDIATE16
  70. ;              ENDM
  71. ;
  72. ;PUSHD         MACRO IMMEDIATE32:REST        ;PUSH 32-bit constant
  73. ;              IF (@WordSize EQ 2)
  74. ;              DB             66H
  75. ;              ENDIF
  76. ;              DB             68H
  77. ;              DD             IMMEDIATE32
  78. ;              ENDM
  79. ;
  80. ;              INCLUDE        XLIBB.INC
  81. ;              INCLUDELIB     XLIBEB.LIB
  82. ;
  83. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  84.  
  85. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  86. ;  Now we will finish our simplified segment directives.
  87. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  88.  
  89.                .STACK         1024
  90.                .DATA
  91.                .CODE
  92.                .STARTUP
  93.  
  94. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  95. ;   Now we have to deal with an annoying complication.  XLIBE.LIB might need 
  96. ;to allocate some conventional memory for its own use.  The problem is that 
  97. ;our program has likely claimed all available memory, even though it isn't 
  98. ;going to use it.  If you are a MASM programmer, then the solution is simple:  
  99. ;Link with the CPARM:1 parameter.  If you are a TASM programmer, then you 
  100. ;must resize the memory block in which this program is contained.  The 
  101. ;following code will do the trick.  This code is also contained in the file
  102. ;RESIZE.INC.
  103. ;
  104. ;              MOV            AX,SP          ;SS:SP = end of program
  105. ;              SHR            AX,4
  106. ;              MOV            BX,SS
  107. ;              ADD            BX,AX
  108. ;              INC            BX             ;BX = first para. beyond program
  109. ;              MOV            AX,ES          ;ES:0000 = first para. of program
  110. ;              SUB            BX,AX          ;BX = program size in para.
  111. ;              MOV            AH,4AH         ;Function to resize memory block
  112. ;              INT            21H            ;Carry will be set if failure
  113. ;              JNC            STILLGOING
  114. ;              MOV            AX,4C01H       ;Failed to resize so terminate
  115. ;              INT            21H
  116. ;STILLGOING:
  117. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  118.  
  119. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  120. ;   One more step and we will be ready to hit protected mode!!!  We must
  121. ;initialize the library by calling INITXLIB.  This procedure will return an
  122. ;error code in EAX.  A code of zero always means success under XLIBE.
  123. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  124.  
  125.                CALL           INITXLIB
  126.                OR             EAX,EAX
  127.                JZ             GOTPOWER
  128.                MOV            AX,4C01H       ;DOS termination function
  129.                INT            21H            ;You will have to read after all.
  130.                                              ;If you simply can't stand user
  131.                                              ;manuals, then try throwing out
  132.                                              ;some device drivers and TSRs.
  133. GOTPOWER:
  134.  
  135. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  136. ;   We don't want to mess around in real mode anymore.  As you will shortly 
  137. ;see, we can do about anything in protected mode that we could do in real 
  138. ;mode, plus a whole lot more.  Moreover, XLIBE makes protected mode easier 
  139. ;than real mode.  We will spend the rest of our time working from a 32-bit 
  140. ;protected-mode subroutine called PMMAIN.  We will get there with an XLIBE 
  141. ;procedure called CALLPM.  Just push the offset of PMMAIN on the stack and 
  142. ;call CALLPM.
  143. ;   There is one big assumption involved here:  It is assumed that PMMAIN and 
  144. ;all other 32-bit routines are contained in a segment called TSEG.  Don't 
  145. ;worry, TSEG can be larger than 64K if you live by the rules.  Read the 
  146. ;manual if you want to know what they are.
  147. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  148.  
  149.                PUSHD          OFFSET PMMAIN  ;Must use a 32-bit offset
  150.